package com.winit.openapi.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.winit.common.spi.context.CommandContext;
import com.winit.common.spring.SpringMessageLocator;
import com.winit.openapi.apiadapter.controller.util.ApiAdapteUtil;
import com.winit.openapi.constants.ApiConstant;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.APISysConfigUtil;
import com.winit.openapi.util.ParameterAdapterUtils;

/**
 * 返回报文统一由CommonController处理
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * kang.wang 	1.0  		2014-12-30 	Created
 * 
 * </pre>
 * @since 1.
 */
@Controller
public class CommonController extends BaseController {
    
    @Resource
    private APISysConfigUtil         apiSysConfigUtil;

    @RequestMapping(value = ERROR)
    public @ResponseBody ResponseMsg api_error(@ModelAttribute ResponseMsg responseMsg) {
        return responseMsg;
    }
    
    @RequestMapping(value = API_ADAPTED_ERROR)
    public @ResponseBody Object api_adapted_error(@ModelAttribute RequestMsg requestMsg,
                                                  @ModelAttribute ResponseMsg responseMsg) {
        String action = requestMsg.getAction();
        // 判断是否需要做错误码适配
        if (apiSysConfigUtil.isNotErrorCodeAdapted(action)) {
            //不需要做错误码适配的直接返回
            return responseMsg;
        }
        // 需要做的进行返回值类型转换
        return ApiAdapteUtil.convertResponse(responseMsg);
        
    }

    @RequestMapping(value = SUCCESS)
    public @ResponseBody ResponseMsg api_success(@ModelAttribute RequestMsg requestMsg,
                                                 @ModelAttribute ResponseMsg responseMsg) {
        String msg = SpringMessageLocator.getMessage(ErrorCode.SUCCESS, CommandContext.getContext().getLocale());
        responseMsg.setMsg(msg);
        // 添加输出参数适配 ,只有API版本是2.0才生效，不会影响现有API add by 2016-07-25
        String version = requestMsg.getVersion();
        if (ApiConstant.API_VERSION_2.equals(version)) {
            return ParameterAdapterUtils.handleOutputAdapte(requestMsg, responseMsg);
        }

        return responseMsg;
    }

    @RequestMapping(value = API_ADAPTED_SUCCESS)
    public @ResponseBody Object apiAdapteSuccess(@ModelAttribute RequestMsg requestMsg,
                                                           @ModelAttribute ResponseMsg responseMsg) {
        String msg = SpringMessageLocator.getMessage(ErrorCode.SUCCESS, CommandContext.getContext().getLocale());
        responseMsg.setMsg(msg);
        
        String action = requestMsg.getAction();
        // 判断是否需要做错误码适配
        if (apiSysConfigUtil.isNotErrorCodeAdapted(action)) {
            //不需要做错误码适配的直接返回
            return responseMsg;
        }
        // 需要做的进行返回值类型转换
        return ApiAdapteUtil.convertResponse(responseMsg);

    }

    @RequestMapping(value = { OPENAPI_SERVICE, ERP_THIRD })
    public String dispatch(@ModelAttribute RequestMsg requestMsg) {
        String action = requestMsg.getAction();
        if (action.contains(".")) {
            action = action.replace(".", "/");
        }
        return action;
    }

}
